let query = require("../../db/mysql.js");
const express = require('express');
const Router = express.Router();
const {create,verify} = require("../token");

//管理员登录
Router.post('/adminlogin', async (req, res) => {
    let { username, userpass } = req.body;
    let data = {};
    try {
        let sql = `select * from admin where name='${username}' and password='${userpass}'`;
        let p = await query(sql);
        if (p.length) {
            let token = create({ name: username, id: p[0].id });
            data = {
                code: 2000,
                flag: true,
                message: '登录成功',
                token:token,
                data: p[0]
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '登录失败',
            }
        }
        res.send(data);
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//管理员修改密码
Router.put('/adminupdataPsw', async (req, res) => {
    let { username, newPsw } = req.body;
    let data = {};
    try {
        let sql = `update admin set password='${newPsw}' where name='${username}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//管理员检测密码是否正确
Router.post('/admincheckPsw', async (req, res) => {
    let { username, userpass } = req.body;
    let data = {};
    try {
        let sql = `select * from admin where password='${userpass}' and name='${username}'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '密码正确',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '密码错误',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

// 获取轮播src
Router.get('/getlunbo', async (req, res) => {
    let data = {};
    try {
        let sql = `select * from src`;
        let p = await query(sql);
        let arr = [];
        let obj =[];
        let num = 0;
        if (p.length) {
            for(let i =0;i<p.length;i++){
                obj.push(p[i]);
                num++;
                if(num==6){
                    arr.push(obj);
                    obj=[];
                    num =0;
                }
            }
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                data:arr
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

//获取新闻
Router.get('/getNews', async (req, res) => {
    let data = {};
    try {
        let sql = `select * from news`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                data:p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})

module.exports = Router;